<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="org.example.pojo.Order" %>
<%@ page import="java.util.List" %>

<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <title>我的订单 - MobileWorld</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css">
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 0;
            padding: 0;
            background-color: #f3f4f6;
            display: flex;
            justify-content: center;
            align-items: flex-start;
            min-height: 100vh;
        }
        header {
            background-color: #0078D7;
            color: white;
            text-align: center;
            padding: 1.5rem;
            animation: fadeInDown 0.8s ease-out;
            border-radius: 10px 10px 0 0;
        }
        header h1 {
            margin: 0;
            font-size: 1.8rem;
        }
        .container {
            max-width: 900px;
            width: 100%;
            margin: 20px auto;
            padding: 2rem;
            background: #ffffff;
            border-radius: 10px;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
            animation: fadeInUp 0.8s ease-out;
        }
        table {
            width: 100%;
            border-collapse: collapse;
            margin-top: 1rem;
        }
        table th, table td {
            text-align: center;
            padding: 10px;
            border: 1px solid #ddd;
        }
        table th {
            background-color: #f4f4f4;
            font-weight: bold;
        }
        table tr:hover {
            background-color: #f9f9f9;
        }
        .actions button {
            padding: 5px 10px;
            background-color: #e3342f;
            color: white;
            border: none;
            border-radius: 3px;
            cursor: pointer;
            transition: background-color 0.3s ease, transform 0.2s ease;
        }
        .actions button:hover {
            background-color: #cc1f1a;
            transform: scale(1.1);
        }
        .no-orders {
            text-align: center;
            color: #555;
            font-size: 1.1rem;
            margin: 2rem 0;
        }
        .back-link {
            text-align: center;
            margin-top: 2rem;
        }
        .back-link a {
            text-decoration: none;
            color: #0078D7;
            font-weight: bold;
            padding: 0.8rem 1.5rem;
            border: 2px solid #0078D7;
            border-radius: 5px;
            transition: all 0.3s ease;
            display: inline-block;
        }
        .back-link a:hover {
            background-color: #0078D7;
            color: white;
        }
        .icon {
            margin-right: 5px;
        }
        /* Toast 样式 */
        .toast {
            position: fixed;
            top: -100px;
            left: 50%;
            transform: translateX(-50%);
            background-color: #ff4d4f;
            color: white;
            padding: 1rem 2rem;
            border-radius: 5px;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
            font-size: 0.9rem;
            opacity: 0;
            transition: all 0.5s ease;
            z-index: 1000;
        }
        .toast.show {
            top: 20px;
            opacity: 1;
        }
        @keyframes fadeInUp {
            from {
                opacity: 0;
                transform: translateY(20px);
            }
            to {
                opacity: 1;
                transform: translateY(0);
            }
        }
        @keyframes fadeInDown {
            from {
                opacity: 0;
                transform: translateY(-20px);
            }
            to {
                opacity: 1;
                transform: translateY(0);
            }
        }
    </style>
</head>
<body>
<%-- Toast 提示信息 --%>
<% if (request.getAttribute("error") != null) { %>
<div class="toast" id="toast">
    <%= request.getAttribute("error") %>
</div>
<% } %>

<div class="container">
    <header>
        <h1><i class="fas fa-shopping-cart icon"></i>我的订单</h1>
    </header>
    <%-- 提示信息显示区域 --%>
    <% if (request.getAttribute("message") != null) { %>
    <div class="toast" style="background-color: #28a745; color: white;" id="toast">
        <%= request.getAttribute("message") %>
    </div>
    <% } %>

    <%-- 订单列表 --%>
    <%
        List<Order> orders = (List<Order>) request.getAttribute("orders");
        if (orders != null && !orders.isEmpty()) {
    %>
    <table>
        <tr>
            <th>订单ID</th>
            <th>商品名称</th>
            <th>数量</th>
            <th>单价 (￥)</th>
            <th>总价 (￥)</th>
            <th>操作</th>
        </tr>
        <% for (Order order : orders) { %>
        <tr>
            <td><%= order.getId() %></td>
            <td><i class="fas fa-box icon"></i><%= order.getProductName() %></td>
            <td><%= order.getQuantity() %></td>
            <td>￥<%= order.getUnitPrice() %></td>
            <td>￥<%= order.getQuantity() * order.getUnitPrice() %></td>
            <td class="actions">
                <form action="orders" method="post" style="display:inline;">
                    <input type="hidden" name="action" value="delete">
                    <input type="hidden" name="orderId" value="<%= order.getId() %>">
                    <button type="submit" onclick="return confirm('确认删除该订单吗？')"><i class="fas fa-trash-alt"></i> 删除</button>
                </form>
            </td>
        </tr>
        <% } %>
    </table>
    <% } else { %>
    <div class="no-orders">
        您目前没有任何订单。
    </div>
    <% } %>
    <div class="back-link">
        <a href="products"><i class="fas fa-arrow-left icon"></i>返回商品页面</a>
    </div>
</div>

<script>
    // Toast 显示与隐藏逻辑
    window.onload = function() {
        const toast = document.getElementById('toast');
        if (toast) {
            toast.classList.add('show');
            setTimeout(() => {
                toast.classList.remove('show');
            }, 3000);
        }
    };
</script>
</body>
</html>
